package com.pack;

import java.util.Iterator;
import java.util.StringTokenizer;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
 
public class DbAdapter {
 
        @SuppressWarnings("unused")
        private static final String LOG_TAG = DbAdapter.class.getSimpleName();
         
        private Context context;
        private SQLiteDatabase database;
        private DatabaseHelper dbHelper;
        
        //Database fields
        private static final String DATABASE_TABLE = "contatti";         
        public static final String KEY_CONTACTID   = "_id";
        public static final String KEY_NOME    = "nome";
        public static final String KEY_COGNOME = "cognome";
        public static final String KEY_EMAIL = "email"; 
        public static final String KEY_NUMERO = "numero"; 
        
        public static final String[] COLONNE=new String[]{KEY_CONTACTID, KEY_NOME, KEY_COGNOME, KEY_EMAIL};
         
        public DbAdapter(Context context) {
            this.context = context;
        }
        public DbAdapter open() throws SQLException { 
            dbHelper = new DatabaseHelper(context);
            database = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() {
            dbHelper.close();                 
        }
        private ContentValues createContentValues(String nome, String cognome, String email, String numero) {
            ContentValues values = new ContentValues();
            values.put( KEY_NOME, nome );
            values.put( KEY_COGNOME, cognome );
            values.put( KEY_EMAIL, email ); 
            values.put(	KEY_NUMERO, numero);
            return values;
        }          
        //create a contact
        public long createContact(String nome, String cognome, String email, String numero) {   
        	if(!getContact(email)) {
                ContentValues initialValues = createContentValues(nome, cognome, email, numero);
                return database.insert(DATABASE_TABLE, null, initialValues);
        	} else return -1;
        }
        //update a contact
        public boolean updateContact( long contactID, String nome, String cognome, String email, String numero ) {     
        	ContentValues updateValues = createContentValues(nome, cognome, email, numero);
            return database.update(DATABASE_TABLE, updateValues, KEY_CONTACTID + "=" + contactID, null) > 0;	
        }        
        //delete a contact     
        public boolean deleteContact(long contactID) { 
            return database.delete(DATABASE_TABLE, KEY_CONTACTID + "=" + contactID, null) > 0;                
        }
        //fetch all contacts
        public Cursor getAllContacts() {                 
            //return database.query(DATABASE_TABLE, COLONNE, null, null, null, null, null);
        	String query = "SELECT * FROM contatti ORDER BY nome ASC";       	 	
       	 	Cursor mCursor=database.rawQuery(query, null);        	 	
       	 	System.err.println("num di righe"+mCursor.getCount());
            return mCursor;   
        }
        //fetch contact
        public Cursor getContact(String nome, String cognome) {
        	 String query = "SELECT * FROM contatti WHERE nome = ? AND cognome = ?";
        	 String[] selectionArgs = { nome, cognome };
        	 Cursor mCursor=database.rawQuery(query, selectionArgs);        	 
        	 System.err.println(""+mCursor.getCount());
             return mCursor;        	
        }
        //fetch contact
        public Cursor getContactsInfo(String nomi) {
        	String query = "SELECT email, numero FROM contatti WHERE ";
        	//tokens
        	StringTokenizer st = new StringTokenizer(nomi, ",");
        	int num = st.countTokens();
        	System.err.println("Num="+num);
        	String[] names = new String[num];
        	
        	names[0]=st.nextToken();
        	System.err.println("Nome="+names[0]);
        	query=query+"nome='"+names[0]+"'";
        	for(int i=1;i<num;i++){
        		names[i] = st.nextToken();
        		System.err.println("Nome="+names[i]);
        		query=query+" OR nome='"+names[i]+"'";        		
        	}
        	
        	System.err.println(query);
        	String[] selectionArgs = { };
       	 	Cursor mCursor=database.rawQuery(query, selectionArgs);        	 
       	 	System.err.println(""+mCursor.getCount());
        	
        	
        	
        	/*
        	 String query = "SELECT email, numero FROM contatti WHERE nome = ?";
        	 String[] selectionArgs = { nome };
        	 Cursor mCursor=database.rawQuery(query, selectionArgs);        	 
        	 System.err.println(""+mCursor.getCount());
        	 */
             return mCursor;        	
        }
        //fetch contact
        public boolean getContact(String email) {
        	 String query = "SELECT _id FROM contatti WHERE email = ?";
        	 String[] selectionArgs = { email };
        	 Cursor mCursor=database.rawQuery(query, selectionArgs);  
             return mCursor.moveToNext();        	
        }
        //fetch contact
        public Cursor getContact(long id) {
        	 String query = "SELECT * FROM contatti WHERE _id = ?";
        	 String[] selectionArgs = { ""+id };
        	 Cursor mCursor=database.rawQuery(query, selectionArgs);  
        	 System.err.println(""+mCursor.getCount());
             return mCursor;        	
        }
}